
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()-1
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

const ajax = (headerType,noLoading, url, data, successCall) => {
  var app = getApp();
  if (noLoading==1){
    wx.showLoading({
      title: '加载中',
    });
  }
  
  var heads = '';
  if (headerType == 0) {
    heads = {
      'content-type': 'application/x-www-form-urlencoded',
      'Cookie': 'PHPSESSID=' + wx.getStorageSync("session_id")
    }
  }
  else {
    heads = {
      'content-type': 'application/x-www-form-urlencoded'
    }
  }
  wx.request({
    url: app.globalData.hosts + url,
    method: 'POST',
    header: heads,
    data: data,
    success: ((res) => {
      wx.hideLoading();
      if (res.statusCode === 200) {
        successCall(res);
      }

    }),
    fail: ((res) => {
      console.log(res);
      if (noLoading == 1) {
        wx.hideLoading();
      }
      wx.showToast({
        title: '网络错误！',
        duration: 2000,
        icon: 'none',
      })
    })
  });
}


module.exports = {
  formatTime: formatTime,
  ajax: ajax
}
